package weaver.formmode.interfaces.impl;

import com.sap.mw.jco.JCO;
import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.jjy.duss.util.SAPConnPool;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-03-10
 * @Description: ${description}
 * @ModifyLog:
 **/

public class GgfybxAction extends AbstractModeExpandJavaCodeNew{

        /**
         * 执行模块扩展动作
         * @param param
         *  param包含(但不限于)以下数据
         *  user 当前用户
         *  importtype 导入方式(仅在批量导入的接口动作会传输) 1 追加，2覆盖,3更新，获取方式(int)param.get("importtype")
         *  导入链接中拼接的特殊参数(仅在批量导入的接口动作会传输)，比如a=1，可通过param.get("a")获取参数值
         * @return
         */
        public Map<String, String> doModeExpand(Map<String, Object> param) {
            Map<String, String> result = new HashMap<String, String>();
            RecordSet rs = new RecordSet();
            RecordSet rs1 = new RecordSet();
            RecordSet rs2 = new RecordSet();
            BaseBean baseBean = new BaseBean();
            try {
                User user = (User)param.get("user");
                int billid = -1;//数据id
                int modeid = -1;//模块id
                RequestInfo requestInfo = (RequestInfo)param.get("RequestInfo");
                if(requestInfo!=null){
                    billid = Util.getIntValue(requestInfo.getRequestid());
                    modeid = Util.getIntValue(requestInfo.getWorkflowid());
                    if(billid>0&&modeid>0){
                        rs.executeQuery("select id,bxgsmc,zfgsmc,jkje,bxje from uf_ggfybx where id =?",billid);
                        while (rs.next()){
                            String mainid = Util.null2String(rs.getString("id"));//mainID
                            String bxgsmc = Util.null2String(rs.getString("bxgsmc"));//报销公司名称
                            String zfgsmc = Util.null2String(rs.getString("zfgsmc"));//支付公司名称
                            int jkje = 0;//借款金额
                            int bxje = 0;//报销金额
                            Map map = new HashMap();
                            map.put("BUTXT",bxgsmc);
                            GgfybxAction ggfybxAction = new GgfybxAction();
                            Map<String,String> resultMap = ggfybxAction.gsdmMethod(map);
                            String bxgsdm = resultMap.get("BUKRS");
                            Map map1 = new HashMap();
                            map1.put("BUTXT",zfgsmc);
                            Map<String,String> resultMap1 = ggfybxAction.gsdmMethod(map1);
                            String zfgsdm = resultMap1.get("BUKRS");
                            baseBean.writeLog("报销人公司代码="+bxgsdm+","+"支付公司代码="+zfgsdm);
                            rs.executeUpdate("update uf_ggfybx set bxgsdm='"+bxgsdm+"',zfgsdm='"+zfgsdm+"' where id =?",billid);
                            baseBean.writeLog("update uf_ggfybx set bxgsdm='"+bxgsdm+"',zfgsdm='"+zfgsdm+"' where id =?",billid);
                            //查明细表
                            rs1.executeQuery("select szxm,gzmc,zffs,bxr,hsje from uf_ggfybx_dt1 where mainid=?",mainid);//明细
                            baseBean.writeLog("select szxm,gzmc,zffs,bxr,hsje from uf_ggfybx_dt1 where mainid=?",mainid);
                            while (rs1.next()){
                                String szxm = Util.null2String(rs1.getString("szxm"));//收支项目
                                String gzmc = Util.null2String(rs1.getString("gzmc"));//柜组名称
                                String zffs = Util.null2String(rs1.getString("zffs"));//支付方式
                                String bxr = Util.null2String(rs1.getString("bxr"));//报销人
                                String hsje = Util.null2String(rs1.getString("hsje"));//含税金额
                                bxje += Util.getIntValue(hsje);
                                //根据报销人带出报销人编号
                                rs2.executeQuery("select workcode from hrmresource where id=?",bxr);
                                baseBean.writeLog("select workcode from hrmresource where id=?",bxr);
                                while (rs2.next()){
                                    String bxrbh = "HH"+Util.null2String(rs2.getString("workcode"));//报销人编号
                                    rs2.executeUpdate("update uf_ggfybx_dt1 set bxrbh='"+bxrbh+"' where mainid=?",mainid);
                                    baseBean.writeLog("update uf_ggfybx_dt1 set bxrbh='"+bxrbh+"' where mainid=?",mainid);
                                }
                                //根据收支项目带出科目编码
                                rs2.executeQuery("select id,szxmmc,hjkmbm from uf_szxmgl where id =?", szxm);
                                baseBean.writeLog("select id,szxmmc,hjkmbm from uf_szxmgl where id =?", szxm);
                                while (rs2.next()){
                                    String hjkmbm = Util.null2String(rs2.getString("hjkmbm"));//科目编码
                                    rs2.executeUpdate("update uf_ggfybx_dt1 set kmbm='"+hjkmbm+"' where mainid=?",mainid);
                                    baseBean.writeLog("update uf_ggfybx_dt1 set kmbm='"+hjkmbm+"' where mainid=?",mainid);
                                }
                                //根据柜组名称带出柜组编号
                                rs2.executeQuery("select id,gzmc,gzbh from uf_gzxx where id=?",gzmc);
                                baseBean.writeLog("select id,gzmc,gzbh from uf_gzxx where id=?",gzmc);
                                while (rs2.next()){
                                    String gzbh = Util.null2String(rs2.getString("gzbh"));//柜组编码
                                    rs2.executeUpdate("update uf_ggfybx_dt1 set gzbh='"+gzbh+"' where mainid=?",mainid);
                                    baseBean.writeLog("update uf_ggfybx_dt1 set gzbh='"+gzbh+"' where mainid=?",mainid);
                                }
                                //根据支付方式带出支付方式编码
                                rs2.executeQuery("select id,zffs,zffsdm from uf_zffsxx where id=?",zffs);
                                baseBean.writeLog("select id,zffs,zffsdm from uf_zffsxx where id=?",zffs);
                                while (rs2.next()){
                                    String zffsdm = Util.null2String(rs2.getString("zffsdm"));//支付方式编码
                                    rs2.executeUpdate("update uf_ggfybx_dt1 set zffsbm='"+zffsdm+"' where mainid=?",mainid);
                                    baseBean.writeLog("update uf_ggfybx_dt1 set zffsbm='"+zffsdm+"' where mainid=?",mainid);
                                }
                            }
                            rs.executeUpdate("update uf_ggfybx set jkje='"+jkje+"',bxje='"+bxje+"' where id =?",billid);
                        }

                    }
                }
            } catch (Exception e) {
                result.put("errmsg","自定义出错信息");
                result.put("flag", "false");
            }
            return result;
        }
    public Map gsdmMethod(Map<String,Object> map) {
        SAPConnPool SAPConn = new SAPConnPool();
        JCO.Client myConnection = SAPConn.getConnection();
        JCO.Function bapi = SAPConn.excuteBapi("Z_IF_FIN115");
        JCO.ParameterList input = bapi.getImportParameterList();//输入参数和结构处理
        JCO.ParameterList inputtable = bapi.getTableParameterList();//输入表的处理

        //入参为表
        JCO.Table t1 = inputtable.getTable("ET_ITAB");
            t1.appendRow();
            t1.setValue(map.get("BUTXT"), "BUTXT");            //公司代码
        //执行函数
        myConnection.execute(bapi);
        JCO.ParameterList out = bapi.getExportParameterList();//输出参数和结构处理
        JCO.ParameterList Table00 = bapi.getTableParameterList();//输出表的处理
        //返回处理结果以及日志
        JCO.Table stu = Table00.getTable("ET_ITAB");
        Map<String, String> mapreturn = new HashMap<>();
        String BUKRS = (String) stu.getValue("BUKRS");//公司代码
        mapreturn.put("BUKRS",BUKRS);
        return mapreturn;
    }

}
